#region Copyright Vanderlande Industries B.V. 2009-2011
//
// Copyright (c) 2009-2011 Vanderlande Industries.
// All rights reserved.
//
// @file:     FscErrorHandler.cs
// @version:  %version: 11 %
// %date_created: Monday, July 23, 2012 3:11:59 PM %
//
// The copyright to the computer program(s) herein is the property of
// Vanderlande Industries. The program(s) may be used and/or copied
// only with the written permission of the owner or in accordance with
// the terms and conditions stipulated in the contract under which the
// program(s) have been supplied.
//
#endregion

using System;
using System.Reflection;
//using VI.Bfw.Common;
//using VI.Bfw.Components;
//using VI.Bfw.Logging;
//using VI.Fsc.OperatorGui.Views.Dashboard;

namespace VI.Fsc.OperatorGui.Models.Application
{
    public delegate void FscErrorEventHandler(bool isFatalError, string context, string title, string userMessage);

    /// <summary>
    /// Handles an FSC error.
    /// </summary>
    public class CFscErrorHandler
    {
        private static CFscErrorHandler instance;

        private bool isDisplayingDialog = false;
        private object mutex = new object();

       // private FscMainForm mainform;
        delegate void HandleErrorCallback(bool isFatalError, string context, string title, string userMessage);
        delegate void CloseApplicationCallback();

        /// <summary>
        /// Getter for the singleton instance of the service.
        /// </summary>
        public static CFscErrorHandler Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new CFscErrorHandler();
                }

                return instance;
            }
        }

        //public FscMainForm MainForm
        //{
        //    get
        //    {
        //        return this.mainform;
        //    }
        //    set
        //    {
        //        this.mainform = value;
        //    }
        //}

        public CFscErrorHandler() {}

        /// <summary>
        /// Handles an error.
        /// </summary>
        /// <remarks>If supplied, an error message is shown. If the error is a fatal one, closing the dialog closes
        /// the application. It may be useful to close the application without message, if a message has been displayed
        /// before.</remarks>
        /// <param name="isFatalError">Indicates whether the error is considered fatal.</param>
        /// <param name="context">The context for the message.</param>
        /// <param name="title">The key of the title of the message dialog.</param>
        /// <param name="messageTextKey">The key of the message. If null or empty no message will be displayed.</param>
        public void HandleError(bool isFatalError, string context, string title, string messageTextKey)
        {
            //Logger.Entering(MethodBase.GetCurrentMethod());

            //if ((null != this.mainform) && this.mainform.InvokeRequired)
            //{
            //    HandleErrorCallback callback = new HandleErrorCallback(HandleError);
            //    this.mainform.Invoke(callback, new object[] { isFatalError, context, title, messageTextKey });
            //}
            //else
            //{
            //    if (!isDisplayingDialog)
            //    //lock (this.mutex)
            //    {
            //        if (!String.IsNullOrEmpty(messageTextKey))
            //        {
            //            //Logger.Info(MethodBase.GetCurrentMethod(), "Error VIDialog opened");

            //            isDisplayingDialog = true;
            //            //VIDialog.ShowErrorDialog(context, title, messageTextKey);
            //            isDisplayingDialog = false;

            //            //Logger.Info(MethodBase.GetCurrentMethod(), "Error VIDialog closed");
            //        }

            //        if (isFatalError)
            //        {
            //            CloseApplication();
            //        }
            //    }
            //}

            //Logger.Exiting(MethodBase.GetCurrentMethod());
        }

        public void CloseApplication()
        {
            bool logoutUser = true;

            //if (null != this.mainform)
            //{
            //    if (this.mainform.InvokeRequired)
            //    {
            //        logoutUser = false;

            //        CloseApplicationCallback callback = new CloseApplicationCallback(CloseApplication);
                    
            //        this.mainform.Invoke(callback);
            //    }
            //    else
            //    {
            //        this.mainform.Close();
            //    }
            //}
            
            if (logoutUser)
            {
                //if (EnvironmentInformation.IsUserLoggedIn)
                //{
                //    EnvironmentInformation.BootStrapper.UserSession.Logout(true);
                //}
            }
        }
    }
}
